"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var Observable_1 = require("rxjs/Observable");
require("rxjs/add/operator/toPromise");
var api_1 = require("../../../config/api");
var AdOssUploadService = /** @class */ (function () {
    function AdOssUploadService(httpService, http) {
        this.httpService = httpService;
        this.http = http;
    }
    // 获取签名
    AdOssUploadService.prototype.getSignature = function (addr) {
        return this.httpService.httpGetAuth("" + api_1.adSystemApiUrl + addr).map(function (data) { return data; });
    };
    AdOssUploadService.prototype.getSignature1 = function (addr, uri, file, el) {
        var formData = new FormData(el);
        formData.append('request_uri', uri);
        formData.append('file[]', file);
        var xhr = new XMLHttpRequest();
        var observable = Observable_1.Observable
            .create(function (observer) {
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    observer.next(JSON.parse(xhr.responseText));
                }
            };
            xhr.open('POST', "" + api_1.adSystemApiUrl + addr);
            xhr.send(formData);
        });
        return observable;
        // const body = JSON.stringify({request_uri: uri, file: file});
        // return this.httpService.httpPostAuth(`${adSystemApiUrl}${addr}`, formData, options).map(data => data);
    };
    AdOssUploadService.prototype.getSuffix = function (filename) {
        var suffix = '';
        var pos = filename.lastIndexOf('.');
        if (pos !== -1) {
            suffix = filename.substring(pos);
        }
        return suffix;
    };
    // 上传文件
    AdOssUploadService.prototype.fileUpload = function (file, data, fileName, para) {
        if (para === void 0) { para = ''; }
        var form = new FormData();
        form.append('key', data.dir + fileName);
        form.append('policy', data.policy);
        form.append('OSSAccessKeyId', data.accessid);
        form.append('success_action_status', '200');
        if (data.callback) {
            form.append('callback', data.callback);
        }
        form.append('signature', data.signature);
        form.append('file', file);
        if (para !== '') {
            form.append('x:field', para);
        }
        return this.http.post(data.host, form).map(function (rs) { return rs; });
    };
    AdOssUploadService = __decorate([
        core_1.Injectable()
    ], AdOssUploadService);
    return AdOssUploadService;
}());
exports.AdOssUploadService = AdOssUploadService;
